import sys
from antlr4 import *
from CPP14Lexer import CPP14Lexer
from CPP14Parser import CPP14Parser

def parse_cpp(file_path):
    # 读取输入文件
    input_stream = FileStream(file_path, encoding='utf-8')
    
    # 词法分析
    lexer = CPP14Lexer(input_stream)
    stream = CommonTokenStream(lexer)
    
    # 语法分析
    parser = CPP14Parser(stream)
    tree = parser.translationUnit()  # 从"translationUnit"规则开始解析
    
    # 打印解析树
    print(tree.toStringTree(recog=parser))

if __name__ == "__main__":
    parse_cpp(sys.argv[1])
